package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorMapper flavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 新增菜品
     * @param
     */
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //向dish表插入一条数据
        dishMapper.insertDishWithFlavor(dish);
        //获取insert语句生成的主键值
        Long dishId = dish.getId();

        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            //先遍历一下口味表 给每一个dishId赋值
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dishId));
            //批量插入
            flavorMapper.addBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult queryPage(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //因为返回值中多了一个categoryName 而Dish中没有 所以又创建一个类DishiVO用于封装返回给前端需要的数据
        Page<DishVO> page = dishMapper.selectPage(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //起售中的菜品不能被删除
        for (Long id : ids) {
             Dish dish =  dishMapper.selectgetStatus(id);
             if(dish.getStatus() == StatusConstant.ENABLE){
                 throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
             }

        }
        //被套餐关联的菜品也不能被删除
        //查哪个表当然要用哪个表的mapper啦
        //一个菜品可能关联多个套餐 所以返回值类型应该为List<Long>

        List<Long> longs = setmealMapper.selectSetmealId(ids);
        if(longs != null && longs.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for(Long id:ids){
            //根据id删除遍历删除
            dishMapper.deleteById(id);
            //删除菜品的同时它关联的口味数据也需要删除
            flavorMapper.delete(id);

        }

    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    public void updateDish(DishDTO dishDTO) {
        //改dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateOnlyDish(dish);
        //改categoryName
        //categoryMapper.updateCategoryName(dishDTO);


        //改flavor 错
        //flavorMapper.updateOnlyFlavor(dishDTO);
        //口味数据值修改类型太多了  可以先删除再新增
        flavorMapper.delete(dishDTO.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            //先遍历一下口味表 给每一个dishId赋值
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dishDTO.getId()));
            //批量插入
            flavorMapper.addBatch(flavors);
        }

    }

    /**
     * 根据id查询菜品用于修改操作的数据回显
     * @param id
     * @return
     */
    public DishVO queryDishById(Long id) {
        DishVO dishVO = dishMapper.selectDishById(id);
        List<DishFlavor> dishFlavor = flavorMapper.selectByDishId(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 根据分类id查询菜品及口味数据
     * @param categoryId
     * @return
     */
    public List<DishVO> queryDishByCategoryId(Long categoryId) {

        List<Dish> dish = dishMapper.selectDishByCategoryId(categoryId);
        List<DishVO> dishVOS = new ArrayList<>();
        dish.forEach(dish1 -> {
            List<DishFlavor> flavor = dishMapper.selectFlavorByDishId(dish1.getId());

            DishVO dishVO = DishVO.builder()
                    .categoryId(categoryId)
                    .categoryName(dish1.getName())
                    .id(dish1.getId())
                    .status(dish1.getStatus())
                    .description(dish1.getDescription())
                    .flavors(flavor)
                    .image(dish1.getImage())
                    .price(dish1.getPrice())
                    .build();

            dishVOS.add(dishVO);
        });

        return dishVOS;
    }

    /**
     * 起售停售菜品
     * @param status
     * @param id
     */
    public void startOrStopDish(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateOnlyDish(dish);
    }
}
